package m1;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @ClassName Consumer
 * @Description:类描述
 * @Author Tedu
 * @Date 2021/6/29 9:06
 * @Version V1.0
 **/
public class Consumer {
    public static void main(String[] args) throws IOException, TimeoutException {
        //1.连接
        ConnectionFactory f = new ConnectionFactory();
        f.setHost("192.168.64.140");
        f.setPort(5672);
        f.setUsername("admin");
        f.setPassword("admin");
        Channel c = f.newConnection().createChannel();
        //2.创建
        c.queueDeclare("helloworld", false, false, false, null);
        //创建回调对象
        DeliverCallback deliverCallback = new DeliverCallback(){

            @Override
            public void handle(String consumerTag, Delivery message) throws IOException {
                //message是消息数据的封装对象
                byte[] a = message.getBody();
                String s = new String(a);
                System.out.println("收到: "+s);
            }
        };
        CancelCallback cancelCallback = new CancelCallback(){

            @Override
            public void handle(String s) throws IOException {
            }
        };
        //3
        c.basicConsume("helloworld", true, deliverCallback,cancelCallback);


    }

}
